#ifndef TESTCOPY_H
#define TESTCOPY_H

//https://www.bilibili.com/video/BV13T4y1P7tw/?spm_id_from=333.337.search-card.all.click&vd_source=16b267e92edd288778342448b69d98f9

#include <iostream>
#include <cstring>

using namespace std;

struct Vector{
   // float x,y;
   double x,y;
};

class String{

private:
    char* m_Buffer;
    unsigned int m_Size;


public:

    String(const char* string)
    {
        m_Size = strlen(string);
        m_Buffer = new char[m_Size + 1];//+1腾出终止字符
        memcpy(m_Buffer, string, m_Size);
        m_Buffer[m_Size] = 0;//+1腾出终止字符
    }
 friend std::ostream & operator <<(std::ostream& stream, const String& string);

 //删除默认拷贝构造函数
 // String(const String& other) = delete;

 /*
 //编译器默认的拷贝构造函数：
 String(const String& other)
 : m_Buffer(other.m_Buffer), m_Size(other.m_Size)
 {

 };*/

 String(const String& other)
 : m_Size(other.m_Size)
 {
    cout <<"copying a string" <<endl;
    m_Buffer = new char[m_Size + 1];
    memcpy(m_Buffer, other.m_Buffer,m_Size+1);
 };


//char operator[](unsigned int index)//lvalue required as left operand of assignment
char& operator[](unsigned int index)
{
    return m_Buffer[index];
}


 ~String(){
 cout << "delete the array new " << endl;
 delete [] m_Buffer;
 }

};

std::ostream & operator <<(std::ostream& stream, const String& string)
{
   // stream << string.m_Size;
    stream << string.m_Buffer;
    return stream;
}


#endif // TESTCOPY_H
